package mysql

import "takeTaxi/common/idl/user"

type Passenger struct {
	Id       int64   `gorm:"column:id;type:int;primaryKey;" json:"id"`
	NickName string  `gorm:"column:nick_name;type:varchar(20);" json:"nick_name"`
	FileId   int64   `gorm:"column:file_id;type:int;" json:"file_id"`
	Mobile   string  `gorm:"column:mobile;type:char(11);" json:"mobile"`
	Sex      string  `gorm:"column:sex;type:varchar(2);" json:"sex"`
	Balance  float32 `gorm:"column:balance;type:float;" json:"balance"`
}

func (p *Passenger) TableName() string {
	return "passenger"
}
func (p *Passenger) FindByMobile(mobile string) (*Passenger, error) {
	err := DB.Where("mobile = ?", mobile).Find(&p).Error
	if err != nil {
		return nil, err
	}
	return p, nil
}

func (p *Passenger) Create(mobile string) error {
	pass := &Passenger{
		NickName: "用户_" + mobile[7:],
		FileId:   1,
		Mobile:   mobile,
		Sex:      "1",
		Balance:  0,
	}
	return DB.Create(&pass).Error
}

func (p *Passenger) UpdateMobile(in *user.UpdateUserMessageReq) error {
	return DB.Model(&p).Where("id=?", in.Mobile).Updates(&Passenger{
		NickName: in.NickName,
		FileId:   in.FileId,
		Mobile:   in.Mobile,
		Sex:      in.Sex,
	}).Error
}
func (p *Passenger) UserRecharge(price float32, userId int64) error {
	return DB.Model(&p).Where("id=?", userId).Update("balance", price+p.Balance).Error
}

func (p *Passenger) UserWithdrawal(price float32, userId int64) error {
	return DB.Model(&p).Where("id=?", userId).Update("balance", p.Balance-price).Error
}
